﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Landsat8_LSTWindow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnLandsatData_Click(object sender, EventArgs e)
        {
            var fbDlg = new FolderBrowserDialog();
            fbDlg.Description = "设置Landsat8数据存储路径";
            if (fbDlg.ShowDialog() == DialogResult.OK)
            {
                txtInPath.Text= fbDlg.SelectedPath;
            }
        }

        private void BtnTIRS_Click(object sender, EventArgs e)
        {
            var fbDlg = new FolderBrowserDialog();
            fbDlg.Description = "设置温度反演数据存储路径";
            if (fbDlg.ShowDialog() == DialogResult.OK)
            {
                txtOutPath.Text = fbDlg.SelectedPath;
            }
        }

        private void ReportProgress(object sender, EventArgs e)
        {
            var args = e as ProgressEventArgs;
            Console.WriteLine(args.Message);
        }


        private async void BtnExecute_Click(object sender, EventArgs e)
        {
            var path = txtInPath.Text;
            var landsatDatasets = Directory.GetDirectories(path, "LC08_*", SearchOption.TopDirectoryOnly);
            var total = landsatDatasets.Count();
            Console.WriteLine(string.Format("待处理Landsat8文件共{0}景", total));
            int i = 1;
            foreach (var l8ds in landsatDatasets)
            {
                var diName = Path.GetFileName(l8ds);
                Console.WriteLine(string.Format("{0}/{1}：开始处理{2}",i, total, diName));
                var mataFile = l8ds + "\\" + diName + "_MTL.txt";
                Console.WriteLine(string.Format("  STEP 1：获取大气纠正参数", i, total));
                var param = await CorrParam.Init(mataFile);
                while (param == null)
                {
                    Console.WriteLine(string.Format("  暂停20s,再次获取大气校正参数", i, total));
                    Thread.Sleep(20 * 1000);
                    param = await CorrParam.Init(mataFile);
                }
                var toolService = new ToolService();
                toolService.OnProgressChanged += ReportProgress;
                var cmd = "\"" + AppDomain.CurrentDomain.BaseDirectory + @"Landsat8_LST.exe" + "\"";
                var inData = l8ds;
                var outData = txtOutPath.Text + "\\" + diName + ".tif";
                var args = string.Format(" \"{0}\" {1} {2} {3} \"{4}\"",
                    inData,
                    param.t,param.Ld,param.Lu,
                    outData);
                Console.WriteLine(string.Format(" STEP 2：计算地表温度", i, total));
                await Task.Factory.StartNew(() => toolService.Run(cmd, args));
                Console.WriteLine(string.Format("{0}/{1}：完成处理{2}", i, total, diName));
                i = i + 1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Console.SetOut(new TextBoxWriter(this.LogBox));
        }
    }
}
